package cn.tedu.store.product.webapi.service.impl;

import cn.tedu.store.commons.pojo.product.dto.AttributeAddNewDTO;
import cn.tedu.store.commons.pojo.product.dto.AttributeUpdateDTO;
import cn.tedu.store.commons.pojo.product.entity.Attribute;
import cn.tedu.store.commons.pojo.product.vo.AttributeListItemVO;
import cn.tedu.store.commons.pojo.product.vo.AttributeStandardVO;
import cn.tedu.store.commons.ex.ServiceException;
import cn.tedu.store.product.webapi.mapper.AttributeMapper;
import cn.tedu.store.product.service.IAttributeService;
import cn.tedu.store.commons.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@DubboService
public class AttributeServiceImpl implements IAttributeService {

    @Autowired
    AttributeMapper attributeMapper;

    @Override
    public void addNew(AttributeAddNewDTO attributeAddNewDTO) {
        log.debug("开始处理添加属性业务 参数:{}",attributeAddNewDTO);
        int count = attributeMapper.countByNameAndTemplateId(attributeAddNewDTO.getName(),attributeAddNewDTO.getTemplateId());
        if(count != 0){
            String message = "添加失败,属性名称已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeAddNewDTO,attribute);
        int rows = attributeMapper.insert(attribute);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void delete(Long id) {
        log.info("开始处理删除属性业务 参数:{}",id);
        AttributeStandardVO attributeStandardVO = attributeMapper.getStandardById(id);
        if (attributeStandardVO == null) {
            String message = "删除失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = attributeMapper.deleteById(id);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void setSaleType(Long id) {
        updateById(id,1,"type");
    }

    @Override
    public void setNormalType(Long id) {
        updateById(id,0,"type");
    }

    @Override
    public void setAllowCustomize(Long id) {
        updateById(id,1,"customize");
    }

    @Override
    public void setNotAllowCustomize(Long id) {
        updateById(id,0,"customize");
    }

    @Override
    public void updateById(Long id, AttributeUpdateDTO attributeUpdateDTO) {
        log.debug("开始处理[修改属性信息]请求 参数:{},{}", id, attributeUpdateDTO);
        AttributeStandardVO queryResult = attributeMapper.getStandardById(id);
        if(queryResult == null){
            String message = "修改属性信息失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeUpdateDTO,attribute);
        //执行修改
        int rows = attributeMapper.updateById(attribute);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public AttributeStandardVO getStandardById(Long id) {
        log.debug("开始处理[查询属性详情]的业务 参数:{}",id);
        AttributeStandardVO result = attributeMapper.getStandardById(id);
        if(result == null){
            String message = "修改属性信息失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return result;
    }

    @Override
    public List<AttributeListItemVO> list() {
        log.debug("开始处理[查询属性列表]的业务 参数:无");
        return attributeMapper.list();
    }

    @Override
    public List<AttributeListItemVO> listByTemplateId(Long TemplateId) {
        log.debug("开始处理[根据属性模板查询属性列表]的业务 参数:{}",TemplateId);
        return attributeMapper.listByTemplateId(TemplateId);
    }

    @Override
    public List<AttributeListItemVO> listBySpuId(Long spuId) {
        return attributeMapper.selectAttributesBySpuId(spuId);
    }


    /*根据id修改属性类型或是否允许自定义*/
    private void updateById(Long id, Integer value,String type){
        String[] typeTips = {"非销售","销售"};
        String[] allowCustomizeTips = {"禁止","允许"};
        boolean flag = "type".equals(type);
        String[] tips = flag?typeTips:allowCustomizeTips;
        AttributeStandardVO queryResult = attributeMapper.getStandardById(id);
        if(queryResult == null){
            String message = "设置"+tips[value]+"属性失败,该属性不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        if((flag?queryResult.getType():queryResult.getIsAllowCustomize()) == value){
            String message = "设置"+tips[value]+"属性失败,该属性已经是"+tips[value]+"属性!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Attribute attribute = new Attribute();
        attribute.setId(id);
        if(flag){
            attribute.setType(value);
        }
        else{
            attribute.setIsAllowCustomize(value);
        }
        int rows = attributeMapper.updateById(attribute);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }
}
